//
//  DYBaseMenuViewController.m
//  BaiSi
//
//  Created by mac on 16/10/27.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYBaseMenuViewController.h"
#import "DYThemeViewController.h"

static NSString * const ID = @"CollectionCell";

@interface DYBaseMenuViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,weak) UIScrollView *titleView;
@property(nonatomic,weak) UICollectionView *contentView;
@property(nonatomic,assign) BOOL isInitial;
@property(nonatomic,weak) UIButton *selectedTitle;
@property(nonatomic,strong) NSMutableArray *titleArrays;
@property(nonatomic,weak) UIView *redLineView;


@end

@implementation DYBaseMenuViewController

-(NSMutableArray *)titleArrays {
    if (_titleArrays == nil) {
        _titleArrays = [NSMutableArray array];
    }
    return _titleArrays;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //设置底部内容View
    [self setUpBottomView];
    
    //设置顶部标题View
    [self setUpTopView];
    
}

#pragma mark - viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //初始化添加子标题
    if (_isInitial == NO) {
        [self addTitles];
        _isInitial  = YES;
    }
    
}

#pragma mark - 添加所有子标题
- (void)addTitles {
    //添加顶部标题
    NSInteger count = self.childViewControllers.count;
    CGFloat btnY = 0;
    CGFloat btnW = DYScreenW / count;
    CGFloat btnH = _titleView.xmg_height;
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIViewController *vc = self.childViewControllers[i];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.frame = CGRectMake(btnW * i, btnY, btnW , btnH);
        btn.tag = i;
        [_titleView addSubview:btn];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleArrays addObject:btn];
        //默认选中第一个按钮
        if (i == 0) {
            [self titleBtnClick:btn];
            //创建红色线条
            UIView *redLineView = [[UIView alloc] init];
            _redLineView = redLineView;
            [_titleView addSubview:redLineView];
            redLineView.backgroundColor = [UIColor redColor];
            
            [btn.titleLabel sizeToFit];
            redLineView.xmg_width = btn.titleLabel.xmg_width;
            redLineView.xmg_height = 2;
            redLineView.xmg_y = _titleView.xmg_height - 2;
            redLineView.xmg_center_x = btn.xmg_center_x;
            
        }
    }
}
#pragma mark - titleBtnClick 点击标题
- (void)titleBtnClick:(UIButton *)button {
    //重复点击,就刷新
    if (button == _selectedTitle) {
        DYThemeViewController *vc = self.childViewControllers[button.tag];
        [vc reload];
    }
    
    [self selectTitle:button];
    //点击标题,移动到相应的view
    CGFloat contentOffset = button.tag * DYScreenW;
    _contentView.contentOffset = CGPointMake(contentOffset, 0);
    
    
}
#pragma mark - 选中按钮
- (void)selectTitle:(UIButton *)button {
    _selectedTitle.selected = NO;
    _selectedTitle = button;
    _selectedTitle.selected = YES;
    
    //移动redLineView
    [UIView animateWithDuration:0.25 animations:^{
        _redLineView.xmg_center_x = button.xmg_center_x;
    }];
}


#pragma mark - 设置顶部标题View
- (void)setUpTopView {
    UIScrollView *scrollVc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 62, DYScreenW, 35)];
    scrollVc.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
    [self.view addSubview:scrollVc];
    _titleView = scrollVc;
    scrollVc.scrollsToTop = NO;
}

#pragma mark - 设置底部内容View
- (void)setUpBottomView {
    //是否自动拉伸内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    //创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    _contentView = collectionView;
    collectionView.scrollsToTop = NO;
    collectionView.backgroundColor = LHColor(215, 215, 215);
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.pagingEnabled = YES;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    [self.view addSubview:collectionView];
    
}

#pragma mark - UICollectionViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = _contentView.contentOffset.x / DYScreenW;
    [self selectTitle:self.titleArrays[page]];
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    //添加内容到cell上
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [cell.contentView addSubview:vc.view];
    
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
